package com.rescue.controller.admin;

import com.rescue.forum.dto.SendMessageDTO;
import com.rescue.forum.dto.UserInfoStatusDTO;
import com.rescue.forum.dto.UserQueryDTO;
import com.rescue.forum.entity.UserMessage;
import com.rescue.mapper.UserMessageMapper;
import com.rescue.result.PageResult;
import com.rescue.result.Result;
import com.rescue.service.UserInfoService;
import com.rescue.service.UserMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "用户信息相关接口")
@RequestMapping("/admin/forum/user")
@Slf4j
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserMessageService userMessageService;

    @PostMapping("/loadUserList")
    @ApiOperation("获取用户列表")
    private Result<PageResult> list(@RequestBody UserQueryDTO userQueryDTO){
        return Result.success(userInfoService.page(userQueryDTO));
    }

    /**
     * 更新用户状态
     * @param userInfoStatusDTO
     * @return
     */
    @PutMapping("/updateUserStatus")
    @ApiOperation("更新用户状态")
    private Result updateUserStatus(@RequestBody UserInfoStatusDTO userInfoStatusDTO){
        log.info("更新用户{}状态为{}",userInfoStatusDTO.getUserId(),userInfoStatusDTO.getStatus()==1?"启用":"禁用");
        userInfoService.updateUserStatus(userInfoStatusDTO.getStatus(),userInfoStatusDTO.getUserId());
        return Result.success();
    }
    @PostMapping("/sendMessage")
    @ApiOperation("向用户发送信息")
    private Result sendMessage(@RequestBody SendMessageDTO sendMessageDTO){
        log.info("向用户发送信息{}",sendMessageDTO);
        //如果积分数量不为空，修改用户积分
        if(sendMessageDTO.getIntegral() != null){
            userInfoService.updateIntegral(sendMessageDTO);
        }
        //新增信息表
        userMessageService.sendMessage(sendMessageDTO);
        return Result.success();
    }
}
